/*
 * Copyright (C) 2017 skydoves
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.skydoves.preferenceroomdemo.utils;

import com.skydoves.preferenceroom.Base64;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.io.UnsupportedEncodingException;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

public class SecurityUtils {
    private static final String CHARSET = "UTF-8";
    private static final String key = "abc12345Bab12345";
    private static final String KEY_ALGORITHM = "AES";
    private static final String DEFAULT_CIPHER_ALGORITHM = "AES/GCM/NoPadding";

    public static String encrypt(String input) {
        if (input == null || input.length() == 0) {
            return input;
        }
        String result = null;
        try {
            byte[] raw;
            raw = key.getBytes(CHARSET);
            SecretKeySpec skeySpec = new SecretKeySpec(raw, KEY_ALGORITHM);
            Cipher cipher = Cipher.getInstance(DEFAULT_CIPHER_ALGORITHM);
            IvParameterSpec IV = new IvParameterSpec(skeySpec.getEncoded(), 0, 16);
            cipher.init(Cipher.ENCRYPT_MODE, skeySpec, IV);
            byte[] encrypted;
            encrypted = cipher.doFinal(input.getBytes(CHARSET));
            result = Base64.encodeToString(encrypted, Base64.DEFAULT);
        } catch (UnsupportedEncodingException
                | NoSuchAlgorithmException
                | InvalidKeyException
                | InvalidAlgorithmParameterException
                | NoSuchPaddingException
                | BadPaddingException
                | IllegalBlockSizeException e) {
            e.printStackTrace();
        }
        return result;
    }

    public static String decrypt(String input) {
        if (input == null || input.length() == 0) {
            return input;
        }
        String result = null;
        byte[] raw;
        try {
            raw = key.getBytes(CHARSET);
            SecretKeySpec skeySpec = new SecretKeySpec(raw, KEY_ALGORITHM);
            Cipher cipher = Cipher.getInstance(DEFAULT_CIPHER_ALGORITHM);
            IvParameterSpec IV = new IvParameterSpec(skeySpec.getEncoded(), 0, 16);
            cipher.init(Cipher.DECRYPT_MODE, skeySpec, IV);
            byte[] encrypted1 = Base64.decode(input, Base64.DEFAULT);
            byte[] original = cipher.doFinal(encrypted1);
            result = new String(original, CHARSET).trim();
        } catch (UnsupportedEncodingException
                | NoSuchAlgorithmException
                | InvalidKeyException
                | InvalidAlgorithmParameterException
                | NoSuchPaddingException
                | BadPaddingException
                | IllegalBlockSizeException e) {
            e.printStackTrace();
        }
        return result;
    }

}